package com.IT.dao.Impl;

import com.IT.dao.UserDao;
import com.IT.entity.User;
import com.IT.utils.DBUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class UserDaoImpl implements UserDao {
    @Override
    public int login(long account, String password) {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        try {
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery("select account, password, authority from t_user where account = " + account);
            if (rs.next()){
                if(rs.getString("password").equals(password)){
                    return Integer.parseInt(rs.getString("authority"));
                }
                else {
                    return -2;  //密码错误
                }
            }else {
                return -1;      //无此账号
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.closeAll(conn, statement, rs);
        }
        return 0;               //异常
    }

    @Override
    public int register(long account, String password, String realName, char sex,
                         long telephone, int authority, String createDate, String onDuty){
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        try{
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery("select account from t_user");
            while (rs.next()){
                long haveAccount = Long.parseLong(rs.getString("account"));
                if(account == haveAccount){
                    //账号已经存在
                    return -1;
                }
            }
            statement.executeUpdate("insert into t_user (account, password, realName, sex, telephone, authority, createDate, onDuty) values ("+account+", '"+password+"', '"+realName +"', '"+sex+"', "+telephone+", "+authority+", '"+createDate+"', '"+onDuty+"')");

/*            rs = statement.executeQuery("select id from t_user where account = "+account);

            int id = 0;
            if(rs.next()){
                id = Integer.parseInt(rs.getString("id"));
            }

            if(authority == 2){
                statement.executeUpdate("insert into depart_user (user_id) values ("+id+")");
            }*/

            //User user = new User(account,password, realName, sex, telephone, authority, createDate, onDuty);
            return authority;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.closeAll(conn, statement, null);
        }
        //数据库操作异常
        return 0;
    }

    @Override
    public int insertLog(long account, String content, String ip, String loginTime) {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        String realName = null;
        int authority = 0;
        try{
            conn = DBUtils.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery("select realName, authority from t_user where account = "+account);
            if(rs.next()){
                realName = rs.getString("realName");
                authority = Integer.parseInt(rs.getString("authority"));
            }
            statement.executeUpdate("insert t_log (account, realName, authority, content, ip, loginTime) values ("+account+", '"+realName+"', "+authority+", '"+content+"', '"+ip+"', '"+loginTime+"')");
            return 1;   //日志登记成功
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.closeAll(conn, statement, rs);
        }
        return 0;   //数据库操作异常
    }
}
